<template>
  <el-form
    ref="form"
    :model="form"
    :rules="rules"
    autocomplete="on"
    label-position="right"
    label-width="95px"
  >
    <div v-for="item in cols" :key="item.colName">
      <el-form-item
        v-if="!(item.colName==='id' && editType==='add') && item.hiddenIn !== editType"
        :label="item.displayName"
        :prop="item.colName"
      >
        <!-- 带眼睛的password -->
        <div v-if="item.dataType==='password'">
          <el-input
            v-model="form[item.colName]"
            :readonly="item.readOnly||readonly"
            :type="passwordType"
          />
          <span class="show-pwd" @click="showPwd(item)">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </div>
        <!-- 默认 -->
        <el-input
          v-else
          v-model="form[item.colName]"
          :readonly="item.readOnly||readonly"
          :type="item.dataType"
        />
      </el-form-item>
    </div>
    <el-form-item>
      <el-button v-if="!readonly" type="primary" @click="onSubmit">确定</el-button>
      <el-button @click="onCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'ResetPassword',
  props: {
    values: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    var validatePassword = (rule, value, callback) => {
      if (value !== '') {
        if (value.length < 8) {
          callback(new Error('请输入至少8位的密码'))
          return false
        } else if (
          !/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,18}$/.test(value)
        ) {
          callback(new Error('密码必须包含字母、数字和特殊字符(@$!%*?&)'))
          return false
        } else {
          callback()
        }
      }
    }
    // var validateSurePassword = (rule, value, callback) => {
    //   if (value !== '') {
    //     if (value.length < 8) {
    //       callback(new Error('请输入至少8位确认密码'))
    //       return false
    //     } else if (value !== this.form.newPassword) {
    //       callback(new Error('两次输入的新密码不一致'))
    //       return false
    //     } else {
    //       callback()
    //     }
    //   }
    // }

    return {
      form: {},
      readonly: false,
      rules: {
        'password': [{ required: true, message: '请输入新密码' }, { validator: validatePassword, trigger: 'blur' }],
        'loginName': [{ required: true, trigger: 'blur', message: '请输入新密码' }]
      },
      editType: 'edit',
      cols: [
        { displayName: 'ID', colName: 'id', readOnly: true },
        { displayName: '登录名', colName: 'loginName', readOnly: true },
        { displayName: '新密码', colName: 'password', dataType: 'password' }
      ],
      passwordType: 'password'
    }
  },
  computed: {
    ...mapGetters([
      'user',
      'name',
      'avatar',
      'roles'
    ])
  },
  watch: {
    values(newValues) {
      this.form = Object.assign({}, newValues)
    }
  },
  created() {
  },
  mounted() {
    this.form = Object.assign({}, this.values)
  },
  methods: {
    onSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.$emit('onSubmit', this.form)
        } else {
          console.log('error validate', this.form)
          this.$message.error('表单内有错误，请检查修改后再提交！')
        }
      })
    },
    onCancel() {
      this.$emit('onCancel')
    },
    showPwd(item) {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    }
  }
}
</script>
<style scoped>
.line{
  text-align: center;
}
.show-pwd {
  position: absolute;
  right: 10px;
  top: 0px;
  font-size: 16px;
  color: #889aa4;
  cursor: pointer;
  user-select: none;
}
</style>
